package com.hog.web.controller.sys;

import com.hog.entity.HogBasOptions;
import com.hog.enums.StateEnum;
import com.hog.service.sys.IHogBasOptionsService;
import com.hog.web.request.PageRequest;
import com.hog.web.response.HogResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Hog系统基本配置管理控制器
 *
 * @author 糖先森
 * @date 2018年03月30日
 * @since JDK 1.8
 */
@Controller
@RequestMapping("/sys")
public class BasOptionsController {
    private static final Logger LOG = LoggerFactory.getLogger(BasOptionsController.class);
    @Autowired
    private IHogBasOptionsService hogBasOptionsService;

    @RequestMapping(value = "/options-list", method = RequestMethod.GET)
    public String optionsList() {
        return "sys/options-list";
    }

    @RequestMapping(value = "/options-json", method = RequestMethod.GET)
    @ResponseBody
    public String json(PageRequest pageReq) {
        List<HogBasOptions> hogBasOptionses = hogBasOptionsService.selectAllByPage(pageReq.getPage(), pageReq.getRows());
        return HogResponse.retDataGridJson(hogBasOptionses);
    }

    /**
     * 新增/更新
     *
     * @param hogBasOptions
     * @return
     */
    @RequestMapping(value = "/saveOrUpdate", method = RequestMethod.POST)
    @ResponseBody
    public HogResponse<?> saveOrUpdate(HogBasOptions hogBasOptions) {
        try {
            hogBasOptionsService.saveOrUpdate(hogBasOptions);
            return HogResponse.ret(StateEnum.SUCCESS);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            return HogResponse.ret(StateEnum.FAIL, e.getMessage());
        }
    }
}